package com.controller.stu;

import com.baseEnum.CourseType;
import com.baseEnum.SexType;
import com.common.convert.Convert;
import com.common.domain.Pageable;
import com.common.utils.CollectionUtil;
import com.entity.Admin;
import com.entity.Course;
import com.entity.SelectCourse;
import com.entity.Student;
import com.service.IStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @Author linjie.jin
 * @Since 2016/1/6
 */
@Controller("studentController")
@RequestMapping("/student")
public class StudentController {

    @Resource
    private IStudentService studentService;

    /**
     * 查询学生的已选的课程信息
     * @param model
     * @param request
     * @return
     * @Author linjie.jin
     */
    @RequestMapping(value = "/courseTable", method = RequestMethod.GET)
    public String selectCourseTable(ModelMap model, HttpServletRequest request) {
        //从session中获取用户
        Object admin = request.getSession().getAttribute("admin");
        String admin_no = null;
        if (admin instanceof Admin) {
            //获取用户名账号
            admin_no = ((Admin) admin).getAdminNo();
            System.out.println(admin_no);
        }
        model.addAttribute("student", studentService.find(admin_no));
        return "/stu/courseTable";
    }

    /**
     * 选课列表
     * @param model
     * @param pageable
     * @return
     * @Author linjie.jin
     */
    @RequestMapping(value = "/selectCourse", method = {RequestMethod.GET, RequestMethod.POST})
    public String selectCourse(ModelMap model, Pageable pageable, CourseType courseType) {
        List<Map> page = studentService.findPage(pageable, courseType);
            model.addAttribute("query", courseType)
                    .addAttribute("courseType", CourseType.values())
                    .addAttribute("page", page);

        return "/stu/selectCourse";
    }

    /**
     * 跳转到增加页面进行选课
     *
     * @param model
     * @param id
     * @return
     * @Author linjie.jin
     */
    @RequestMapping(value = "/viewCourse", method = {RequestMethod.GET, RequestMethod.POST})
    public String viewCourse(ModelMap model, Integer id) {
        model.addAttribute("stu", studentService.getDetailCourse(id));
        return "/stu/save";
    }

    /**
     * 查看自己的分数
     * @return
     * @Author linjie.jin
     */
    @RequestMapping(value = "/viewGrade", method = RequestMethod.GET)
    public String viewGrade(HttpServletRequest request) {
        //从session中获取用户
        Object admin = request.getSession().getAttribute("admin");
        String admin_no = null;
        if (admin instanceof Admin) {
            //获取用户名账号
            admin_no = ((Admin) admin).getAdminNo();
        }
        studentService.selectGrade(admin_no);
        return "/stu/viewGrade";
    }

    /**
     * 保存学生所选课程
     *
     * @param selectCourse
     * @return
     * @author ri.yang
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    public String save(SelectCourse selectCourse, HttpServletRequest request) {
        //从session中获取用户
        Object admin = request.getSession().getAttribute("admin");
        String admin_no = null;
        if (admin instanceof Admin) {
            //获取用户名账号
            admin_no = ((Admin) admin).getAdminNo();
        }
        //将学生学课信息保存
        selectCourse.setStuNo(admin_no);
        studentService.save(selectCourse);
        return "redirect:selectCourse.html";
    }

    /**
     * 编辑学生的维护信息
     *
     * @param modelMap
     * @param request
     * @param id
     * @return
     * @author foshou.zhang
     */
    @RequestMapping("/edit")
    public String edit(ModelMap modelMap, HttpServletRequest request, String id) {
        String stuNo = request.getParameter("id");
        Student student = studentService.getStudentByStuNo(stuNo);
        modelMap.addAttribute("student", student);
        return "stu/edit";
    }

    /**
     * 更新学生的维护信息
     *
     * @return
     * @author foshou.zhang
     */
    @RequestMapping("/update")
    public String update(Student student, String gender) {
        try {
            student.setName(new String(student.getName().getBytes("ISO-8859-1"), "UTF-8"));
            student.setClasses(new String(student.getClasses().getBytes("ISO-8859-1"), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        student.setSex(SexType.getSexType(gender));
        studentService.updateStudent(student);
        return "redirect:/maintain/studentMaintain.html";
    }

}
